package com.outstanding.ifml.diff;

import com.outstanding.ifml.bo.FieldType;

/**
 * 差异
 * @author chenzhixuan
 *
 */
public class DiffItem {
	
	private String oldLiner;
	public String getOldLiner() {
		return oldLiner;
	}

	public void setOldLiner(String oldLiner) {
		this.oldLiner = oldLiner;
	}

	public String getNewLiner() {
		return newLiner;
	}

	public void setNewLiner(String newLiner) {
		this.newLiner = newLiner;
	}

	public DiffType getDiffType() {
		return diffType;
	}

	public void setDiffType(DiffType diffType) {
		this.diffType = diffType;
	}

	private String newLiner;
	
	// 相对与old的变化差异，比如old有，newliner无，则为delete；
	//old 无，newliner有，则为insert；
	//其余的为 update
	private DiffType diffType;
	
	
	private String liner;
	private String itemName;
	private FieldType fieldType= FieldType.other;;
	
	
	/**
	 * 将本身的差异信息转换为 对应的Item 和 DiffType 与 String oldValue;  newsValue;
	 */
	public void convert(){
		if(this.oldLiner != null && this.oldLiner.trim().length() >0){
			this.liner = this.oldLiner;
		}else{
			this.liner = this.newLiner;
		}
		parserItemName();
		parserFieldType();
	}
	
	
	private void parserItemName(){
		this.liner = this.liner.trim();
		int index = this.liner.indexOf("(");
		int colonindex = this.liner.indexOf(":");
		if(index != -1){
			this.itemName = this.liner.substring(0, index);
		}else if (colonindex != -1){
			this.itemName  = this.liner.substring(0, colonindex);
		}else{
			this.itemName  = this.liner;
		}
		
	}
	
	private void parserFieldType(){
		int index = this.liner.indexOf("fieldType");
		if(index != -1){
			String restLiner = this.liner.substring(index+11);
			int endIndex = restLiner.indexOf("\"");
			String value = restLiner.substring(0,endIndex);
			this.fieldType =  FieldType.valueOf(value);
			
		}
	}
	
	public String getItemName() {
		return itemName;
	}

	public FieldType getFieldType() {
		return fieldType;
	}

	@Override
	public String toString() {
		return "DiffItem [oldLiner=" + oldLiner + ", newLiner=" + newLiner + ", diffType=" + diffType + "]";
	}
	
	
	
}
